package noUsed;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import general.Mechanisme;
import graphique.C;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

public class Dessinateur implements GLEventListener, MouseMotionListener, MouseWheelListener
{
	Mechanisme m;
	
	GL gl;

	int selectionne = -1;
	int regarde = -1;

	double angleX = 0;
	double angleY = 45;
	
	int TERRAIN = 1;
	
	double modifRelief = 50;
	
	int posMX;
	int posMY;
	
	double ech = 200;
	
	GLU glu = new GLU();
	
	int h,w;
	
	double zoom =200000;
	
	public Dessinateur( Mechanisme m )
	{
		this.m = m;
		
		h = C.taille;
		w = C.taille;
	}
	
	private void constructionDisplayList( GL gl )
	{
		
		double limiteEau = 0.5;
		double limiteSable = 0.55;
		
		double baseR = 5;
		
		double r;
		
		double theta;
		double phi;
		
		double x;
		double y;
		double z;
		
		System.out.println("Construction GL debut");
		
		gl.glNewList( TERRAIN , GL.GL_COMPILE );
		
		for( int i = 0 ; i < m.g.length-1 ; i++ )
		{
			gl.glBegin(gl.GL_QUAD_STRIP );
			
			for( int j = 0 ; j < m.g[0].length/2  ; j += 2 )
			{
				if( (float) m.g[0][i][j] < limiteEau )
				{
					gl.glColor3f( 0 ,0 ,(float) (m.g[0][i][j]/limiteEau) );
				}
				else if( (float) m.g[0][i][j] < limiteSable )
				{
					gl.glColor3f( 1-(float) ((m.g[0][i][j]-limiteEau)/(limiteSable)) ,1-(float) ((m.g[0][i][j]-limiteEau)/(limiteSable)) , 0 );
				}
				else
				{
					gl.glColor3f( 0 , 1-(float) ((m.g[0][i][j]-limiteEau)/(1-limiteEau)) ,0 );
				}
				
				r = -(m.g[0][i][j] + baseR) * modifRelief;
				
				theta = (double)i/m.g.length  * Math.PI;
				phi = (double)j/m.g[0].length * Math.PI;
				
				x = r * Math.cos( theta ) * Math.cos( phi );
				y = r * Math.cos( theta ) * Math.sin( phi );
				z = r * Math.sin( theta );
				
				gl.glVertex3d( x , y , z );
				
				if( (float) m.g[0][i+1][j] < limiteEau )
				{
					gl.glColor3f( 0 ,0 ,(float) (m.g[0][i+1][j]/limiteEau) );
				}
				else if( (float) m.g[0][i+1][j] < limiteSable )
				{
					gl.glColor3f( 1-(float) ((m.g[0][i+1][j]-limiteEau)/(limiteSable)) ,1-(float) ((m.g[0][i+1][j]-limiteEau)/(limiteSable)) , 0 );
				}
				else
				{
					gl.glColor3f( 0 , 1-(float) ((m.g[0][i+1][j]-limiteEau)/(1-limiteEau)) ,0 );
				}
				
				r = -(m.g[0][i+1][j] + baseR) * modifRelief;
				
				theta = (double)(i+1)/m.g.length * Math.PI;
				phi = (double)j/m.g[0].length * Math.PI;
				
				x = r * Math.cos( theta ) * Math.cos( phi );
				y = r * Math.cos( theta ) * Math.sin( phi );
				z = r * Math.sin( theta );
				
				gl.glVertex3d( x, y, z);
			}
			gl.glEnd();
		}
		gl.glEndList();
		
		System.out.println("Construction GL fin");
	}

	public void display( GLAutoDrawable drawable )
	{
		GL gl=drawable.getGL();
		
	    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glScalef(C.taille/w, C.taille/h, 1);
		
		GLU glu=new GLU();

		gl.glScalef(0.58f,0.58f,0.58f);
		glu.gluPerspective(50.0f, 1, 0, 200.0);
		glu.gluLookAt( 0f, 0f, -zoom ,0, 0 , 0, 0, 1, 0);
		
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();

		gl.glColor3d( 1, 1, 1);
		
		m.avancement();
		
		gl.glScaled( ech, ech, ech);
		
		gl.glRotated( angleY , 1 , 0 , 0 );
		gl.glRotated( angleX , 0 , 0 , 1 );
		
		//gl.glTranslatef( -m.g.length/2 , -m.g[0].length/2 , 0 );
		
		//System.out.println(angleX + " " + angleY);
		
		//System.out.println("azerty " + angleX );
		
		gl.glCallList(TERRAIN);
		gl.glFlush();
		
        try
        {
                Thread.sleep(C.vitesse);
        } catch (InterruptedException e)
        {
                e.printStackTrace();
        }
        
        //gl.glEnable(GL.GL_LIGHTING);
        
	}

	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) 
	{

	}

	public void init(GLAutoDrawable drawable )
	{
		GL gl = drawable.getGL();

		gl.glShadeModel(GL.GL_SMOOTH);              // Enable Smooth Shading
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    // Black Background
		gl.glClearDepth(1.0f);                      // Depth Buffer Setup
		gl.glEnable(GL.GL_DEPTH_TEST);							// Enables Depth Testing
		gl.glDepthFunc(GL.GL_LEQUAL);								// The Type Of Depth Testing To Do
		gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);	// Really Nice Perspective Calculations

		constructionDisplayList( gl );
	}

	public void reshape(GLAutoDrawable drawable , int arg1, int arg2  , int width, int height) 
	{
		GL gl = drawable.getGL();
		
		/*
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		
		//glu.gluPerspective((float) C.taille / (float) C.taille , 60.0 , 0, 100);
		
		gl.glOrtho( -1 , 1 , 1 , -1 , 1 , 1 );

		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		*/
	}

	public void mouseDragged(MouseEvent arg0)
	{
		double modifAngle = 130;
		
		int difX = posMX - arg0.getX();
		int difY = posMY - arg0.getY();
		
		double difAngleX = -(double)difX/C.taille * modifAngle;
		double difAngleY = (double)difY/C.taille * modifAngle;
		
		angleX += difAngleX;
		angleY += difAngleY;
		
		posMX = arg0.getX();
		posMY = arg0.getY();
	}

	public void mouseMoved(MouseEvent arg0)
	{
		posMX = arg0.getX();
		posMY = arg0.getY();
		
		
		//arg0.
	}

	public void mouseWheelMoved(MouseWheelEvent arg0)
	{
		if( arg0.getWheelRotation() > 0 )
		{
			zoom *= 0.9;
		}
		else
		{
			zoom *= 1.1;
		}
	}
}
